package com.testDemo.hutool;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.RandomUtil;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class ListUtilsTest {
    @Test
    public void sub() {
        String file = "/app/snow/test";
        int c = 0, m = 0;
        boolean flag = true;
        do {
            c = RandomUtil.randomInt(0, 2);
            m = RandomUtil.randomInt(0, 2);
            String key = c + ":" + m;

            List<String> list = null;
            try {
                list = FileUtil.readLines(file, StandardCharsets.UTF_8);
            } catch (IORuntimeException e) {
                list = new ArrayList<>();
                System.err.println("File not exist: "+file);
            }
            int len = list.size(), start = 0;
            if (len > 3) {
                start = len - 3;
            }
            if (len > 0) {
                final List<String> sub = ListUtil.sub(list, start, len);
                if (sub.contains(key)) {
                    continue;
                }
            }

            FileUtil.appendUtf8String(key+"\n", file);
            flag = false;
        } while (flag);
    }
}
